<?php

namespace app\admin\controller;

use think\Db;

class Bonus extends Home
{
    /**
     * 奖金统计
     */
    public function bonusTotal()
    {
        $where = $this->getMap('count_date');
        $map = $where['map'];
        $maps = $where['maps'];
        $usernumber = input('usernumber');
        if (!empty($usernumber)) {
            $maps['usernumber'] = $usernumber;
            $userid = Db('member')->where('usernumber', $usernumber)->value('uid');
            if ($userid) {
                $map['userid'] = $userid;
            } else {
                $this->error('该会员不存在！');
            }
        }

        $list = Db('bonus_total')->where($map)->order('count_date desc')->paginate(20, false, ['query' => $maps]);
        $page = $list->render();
        $list = $this->getAll($list);
        $this->assign('_list', $list);
        $this->assign('page', $page);

        $arr = array(
            array(
                'type' => 'input',
                'name' => 'usernumber',
                'inputid' => '',
                'readonly' => '',
                'title' => '会员账号',
                'value' => $usernumber,
            ),
        );
        $this->searchCondition($maps, $arr);

        $this->assign('search', url('bonusTotald'));
        $this->assign('title', '奖金累计');
        return $this->fetch('bonustotal');
    }

    public function bonusTotald()
    {
        $where = $this->getMap('count_date');
        $map = $where['map'];

        $list = Db('bonus_total')->where($map)->order('count_date desc')->select();
        $title = "奖金累计";
        $a = array(
            array('date', '汇总时间'),
            array('usernumber', '会员编号'),
            array('realname', '会员姓名'),
            array('bonus1', '影响力价值'),
            array('bonus2', '管理奖'),
            array('bonus3', '管理奖'),
            array('bonus4', '分红'),
            array('total', '累计奖金')
        );
        foreach ($list as $k => &$v) {
            $v['date'] = date("Y-m-d H:i:s", $v['count_date']);
            $v['usernumber'] = getInfo('usernumber', $v['userid']);
            $v['realname'] = getInfo('realname', $v['userid']);
        }

        exportExcel($title, $a, $list);
    }

    /**
     * 奖金明细
     */
    public function showMore()
    {
        $where = $this->getMap('createtime');
        $map = $where['map'];
        $maps = $where['maps'];
        $targetuserid = input('targetuserid');
        $targetusernumber = input('targetusernumber');
        if (!empty($targetusernumber)) {
            $maps['targetusernumber'] = $targetusernumber;
            $map['targetuserid'] = Db('member')->where('usernumber', $targetusernumber)->value('uid');
        }
        if (!empty($targetuserid)) {
            $maps['targetuserid'] = $map['targetuserid'] = $targetuserid;
            $targetusernumber = Db('member')->where('uid', $targetuserid)->value('usernumber');
        }

        $changetype = input('changetype');

        $maps['recordtype'] = $map['recordtype'] = 1;
        if ($changetype > 0) {
            $maps['changetype'] = $map['changetype'] = $changetype;
        } else {
            $map['changetype'] = array('in', '1,2,3,4,5,6');
        }

        $list = Db('money_change')->where($map)->order('id desc')->paginate(20, false, ['query' => $maps]);
        $page = $list->render();
        $list = $list->toArray();
        $this->assign('_list', $list['data']);
        $this->assign('page', $page);

        /*查询条件*/
        $arr = array(
            array(
                'type' => 'select',
                'name' => 'changetype',
                'option' => array(
                    '0' => '全部',
                    '1' => getChangeType(1),
                    '2' => getChangeType(2),
                    '3' => getChangeType(3),
                    '4' => getChangeType(4),
                    '5' => getChangeType(5),
                    '6' => getChangeType(6),
                    '7' => getChangeType(7),
//                    '10' => getChangeType(10),
//                    '11' => getChangeType(11),
//                    '12' => getChangeType(12),
//                    '13' => getChangeType(13)
                ),
                'inputid' => '',
                'readonly' => '',
                'title' => '奖金类型',
                'value' => $changetype,
            ),
            array(
                'type' => 'input',
                'name' => 'targetusernumber',
                'inputid' => '',
                'readonly' => '',
                'title' => '会员账号',
                'value' => $targetusernumber,
            )
        );

        $this->searchCondition($maps, $arr);
        $this->assign('title', '奖金明细');
        return $this->fetch('showmore');
    }

    /**
     * 奖金设置
     * @param int $id
     */
    public function bonusRule($id = 1)
    {
        if (request()->isPost()) {
            $data = input('post.');
            if ($data) {
                $code = input('code1');
                if ($code != session('email_code')) {
                    $this->error('邮箱验证码错误');
                }

                unset($data['code1']);
                $res = Db('bonus_rule')->where('id', 1)->update($data);
                if ($res) {
                    session('email_code', null);
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        } else {
            /* 获取数据 */
            $info = Db('BonusRule')->find($id);
            if (empty($info)) {
                $this->error('获取后台菜单信息错误');
            }
            $this->assign('info', $info);

            if (!self::$adminInfo['email']) {
                $this->error('缺少邮箱，不可编辑');
            }

            $this->assign('email', self::$adminInfo['email']);
            $this->assign('title', '奖金管理');
            return $this->fetch('bonusrule');
        }
    }

    /**
     * 分红发放
     * @return bool
     */
    public function bonusDivid()
    {
        if (request()->isPost()) {

            $code = input('code');
            if ($code != session('email_code')) {
                $this->error('邮箱验证码错误');
            }
            session('email_code', null);

            $total = input('total');
            if ($total <= 0) {
                $this->error('参数错误');
            }

            $members = Db('member')->where('status', 1)->where('level', 4)->where('is_divid2', 1)->select();
            if (!$members) {
                $this->error('无合伙人');
            }

            $num = count($members);
            $bonusMoney = round($total / $num, 2);
            $bonus = new \app\common\api\controller\Bonus();
            foreach ($members as $v) {
                $bonus->bonusDivid($v['uid'], $bonusMoney);
            }

            Db('divid_count_profile')->where('status', 0)->update(['status' => 1, 'finish_time' => time()]);

            $this->success('发放成功');
        }
    }
}